package CGW.controller;

import CGW.bean.Food;
import CGW.bean.Page;
import CGW.dao.FoodDao;
import CGW.service.FoodService;
import CGW.utils.ZHI;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/food")
public class FoodServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        FoodService service = new FoodService(new FoodDao());
        String action = req.getParameter("action");
        if (action == null){
            String page = req.getParameter("page");
            int pageInt = 1;
            String pageSize = req.getParameter("pageSize");
            int pageSizeInt = 20;
            if(page != null){
                pageInt = Integer.parseInt(page);
            }
            if (pageSize != null){
                pageSizeInt = Integer.parseInt(pageSize);
            }

            Page<Food> pageBean1= service.queryAll(pageInt, pageSizeInt);
            System.out.println(pageBean1.getDatas());
            req.setAttribute("pageBean1",pageBean1);
            req.getRequestDispatcher("food.jsp").forward(req,resp);

        }else if(action.equals("update")){
            String food_id = req.getParameter("food_id");
            // 依据user_id 获取用户的信息
            Food food = service.get(Integer.parseInt(food_id));
            req.setAttribute("food", food);
            req.getRequestDispatcher("food_update.jsp").forward(req, resp);
        }else if(action.equals("delete")){
            String food_id = req.getParameter("food_id");
            boolean ret = service.delete(Integer.parseInt(food_id));
            if(ret){
                req.getSession().setAttribute("delMsg", "删除成功");
            }else{
                req.getSession().setAttribute("delMsg", "删除失败");
            }
            resp.sendRedirect("/food");
        }else if(action.equals("add")){
            service.close();
            Food food=new Food();
            req.setAttribute("food", food);
            req.getRequestDispatcher("food_update.jsp").forward(req, resp);
        }else if(action.equals("delMsg")){
            // 异步请求及响应
            req.getSession().removeAttribute("delMsg");
            resp.getWriter().println("ok");
        }



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        FoodService service = new FoodService(new FoodDao());

        String type_id = req.getParameter("type_id");
        String food_name = req.getParameter("food_name");
        String food_price = req.getParameter("food_price");
        String food_mprice = req.getParameter("food_mprice");
        String food_image = req.getParameter("food_image");
        String food_desc = req.getParameter("food_desc");
        String food_id = req.getParameter("food_id");
        System.out.println(type_id);
        boolean ret = false;
        Food food = null;
        if(food_id.equals("")){
            // 新增
            food = new Food(ZHI.zh(type_id),food_name,Double.parseDouble(food_price),Double.parseDouble(food_mprice),food_image,food_desc);
            ret = service.insert(food);
            resp.sendRedirect("/food");
        }else{
            food = new Food(ZHI.zh(food_id),ZHI.zh(type_id),food_name,Double.parseDouble(food_price),Double.parseDouble(food_mprice),food_image,food_desc);
            ret = service.update(food);
            if(ret){
                resp.sendRedirect("/food");
            }else{
                req.setAttribute("food", food);
                req.setAttribute("error", food_id.equals("0")?"新增失败":"更新失败");
                req.getRequestDispatcher("/food_update.jsp").forward(req, resp);
            }
        }
    }
}
